//
//  SearchViewController.m
//  homeland
//
//  Created by rain on 14-5-21.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "SearchViewController.h"
#import "IconPagesViewController.h"
#import "WorkQueryViewController.h"
#import "SearchView.h"
#import "SegmentView.h"

#import "ListViewController.h"
@interface SearchViewController ()<SearchViewDelegate,SegmentViewDelegate,IconPagesViewControllerDelegate>
{
    int segmentIndex;
}
@property (nonatomic, strong) IconPagesViewController *resourceView;
@property (nonatomic, strong) IconPagesViewController *guideView;

@property (nonatomic, weak) IBOutlet UIButton *resourceBtn;
@property (nonatomic, weak) IBOutlet UIButton *guideBtnBtn;

@end

@implementation SearchViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    SearchView *searchView = [SearchView initView];
    searchView.delegate = self;
    [searchView setFrame:CGRectMake(0, 20, 320, 43)];
    [self.view addSubview:searchView];
    
    NSArray *array = @[@{@"image":@"查资源",@"light":@"查资源_选中"},
                       @{@"image":@"查指南",@"light":@"查指南_选中"}];
    SegmentView *segmentView = [[SegmentView alloc] initWithFrame:CGRectMake(0, 63, 320, 29)];
    segmentView.delegate = self;
    [segmentView initView:array];
    [self.view addSubview:segmentView];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"IconPages" ofType:@"plist"];
    NSDictionary *dictionary = [[NSDictionary alloc]initWithContentsOfFile:plistPath];
    
     self.resourceView = [[IconPagesViewController alloc]initWithNibName:@"IconPagesViewController" bundle:nil];
    self.resourceView.config = [dictionary objectForKey:@"按资源"];
    self.resourceView.delegate = self;
    [self.resourceView.view setFrame:CGRectMake(0, 92, 320, 568 - 92)];
    [self.view addSubview:self.resourceView.view];
    
    
    
    self.guideView = [[IconPagesViewController alloc]initWithNibName:@"IconPagesViewController" bundle:nil];
    self.guideView.config = [dictionary objectForKey:@"查指南"];
    [self.guideView.view setFrame:CGRectMake(0, 92, 320, 568 - 92)];
    self.guideView.delegate = self;

    [self.view addSubview:self.guideView.view];
    
    self.guideView.view.hidden = YES;
    
    self.resourceBtn.selected = YES;
}

- (void)didSelectIconViewControll:(Class )tempClass nib:(NSString *)nibName config:(NSDictionary *)config
{
    ConfigableViewController *vc = [[tempClass alloc]initWithNibName:nibName bundle:nil];
    vc.config = config;
    vc.hidesBottomBarWhenPushed = YES;
    self.navigationController.navigationBarHidden = NO;

    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didSearchText:(NSString *)text
{
    if ([text length] > 0) {
        ListViewController *vc = [[ListViewController alloc]initWithNibName:@"ListViewController" bundle:nil];
        vc.searchText = text;
        vc.hidesBottomBarWhenPushed = YES;
        NSMutableDictionary *vcConfig = [[NSMutableDictionary alloc] init];
        [vcConfig setObject:text forKey:@"key"];
        [vcConfig setObject:[NSNumber numberWithBool:YES] forKey:@"MyConcern"];
        [vcConfig setObject:text forKey: @"fastSearchKey"];
        [vcConfig setObject:segmentIndex == 0 ? @"MyConcernService" : @"MYGuideCommonService" forKey:@"myGudieServe"];
        [vcConfig setObject:segmentIndex == 0 ? @"InfoWithPhoneAndMapCell" : @"WorkQueryCell" forKey:@"myGudieCell"];

        vc.config = vcConfig;
        self.navigationController.navigationBarHidden = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }

}

- (void)didSelectSegmentAtIndex:(int)index
{
    if (index == 0) {
        self.resourceView.view.hidden = NO;
        self.guideView.view.hidden = YES;
    } else {
        self.resourceView.view.hidden = YES;
        self.guideView.view.hidden = NO;

    }
    segmentIndex = index;
}



- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
